package com.example.gomoku;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.SQLException;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import com.example.gomoku.DBHelper;

public class LoginActivity extends AppCompatActivity implements View.OnClickListener {

    private EditText et_id;
    private EditText et_pwd;
    private CheckBox cb_save;
    private CheckBox cb_login;
    private SharedPreferences prefs;
    private SharedPreferences.Editor editor;

    //oncreate:创建activity实例
    //onstart:显示页面
    //onresume:获得焦点
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate( savedInstanceState );
        setContentView( R.layout.activity_login );

        initView();
        prefs = getSharedPreferences("user", Context.MODE_PRIVATE );
        editor = prefs.edit();
        loadInfo();
    }

    private void loadInfo() {
        String id = prefs.getString( "uid","" );
        String password = prefs.getString( "upassword","" );
        boolean isSave = prefs.getBoolean( "isSave",false );
        boolean isAuto = prefs.getBoolean( "isAuto",false );

        if(isSave){
            et_id.setText( id );
            et_pwd.setText( password );
            cb_save.setChecked( isSave );
            cb_login.setChecked( isAuto );
        }
        if (isAuto) {
            new Handler().postDelayed( new Runnable() {
                @Override
                public void run() {
                    Intent i = new Intent(LoginActivity.this, MainActivity.class);
                    startActivity(i);
                    finish();
                }
            }, 1000); // 延迟1秒后跳转
        }

    }


    //页面控件初始化
    void initView(){
        et_id = findViewById(R.id.et_uid );
        et_pwd = findViewById( R.id.et_upwd );
        cb_save = findViewById( R.id.cb_save );
        cb_login = findViewById( R.id.cb_login );
        findViewById( R.id.btn_login ).setOnClickListener( this );
        findViewById( R.id.btn_register ).setOnClickListener( this );
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.btn_login) {
            handleLogin();
        } else if (id == R.id.btn_register) {
            Intent i = new Intent( LoginActivity.this, RegisterActivity.class );
            startActivity( i );
        }
    }

    private void handleLogin() {
        String uid = et_id.getText().toString();
        String upwd = et_pwd.getText().toString();
        boolean isSave = cb_save.isChecked();
        boolean isAuto = cb_login.isChecked();

        if (uid.isEmpty() || upwd.isEmpty()) {
            Toast.makeText(this, "请输入用户名和密码", Toast.LENGTH_SHORT).show();
            return;
        }

        try {
            DBHelper dbHelper = new DBHelper(this);
            String uname = dbHelper.verifyUser(uid, upwd);
            if (!uname.isEmpty()) {
                // 数据存储
                if (isSave) {
                    editor.putString("uid", uid);
                    editor.putString("upassword", upwd);
                    editor.putBoolean("isSave", isSave);
                    editor.putBoolean("isAuto", isAuto);
                    editor.commit();
                    Toast.makeText(this, "保存成功", Toast.LENGTH_SHORT).show();
                }
                Toast.makeText(this, "登录成功", Toast.LENGTH_SHORT).show();
                Intent i = new Intent(LoginActivity.this, MainActivity.class);
                i.putExtra("uname", uname);
                i.putExtra("uid", uid);
                startActivity(i);
                finish();
            } else {
                Toast.makeText(this, "登录失败：用户名或密码错误", Toast.LENGTH_LONG).show();
            }
        } catch (SQLException e) {
            e.printStackTrace();
            Toast.makeText(this, "数据库错误", Toast.LENGTH_LONG).show();
        }
    }
}